﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using nStruct;

namespace nUtil
{
	class cUtil
	{
		public static void LDS1000_Structure_Copy(ref PS_Conf_Struct ret, PS_Conf_Struct inDat)
		{
			ret.FileName = inDat.FileName;
			ret.FolderName = inDat.FolderName;
			ret.FullFileName = inDat.FullFileName;
			ret.LogFileName = inDat.LogFileName;

			ret.SelectItem = inDat.SelectItem;
			ret.PsSelectCh = inDat.PsSelectCh;
			ret.TempSelectCh = inDat.TempSelectCh;

			ret.START = inDat.START;
			
			ret.PsNo = inDat.PsNo;
			ret.IProtect_Master = inDat.IProtect_Master;
			ret.VsetMaster = inDat.VsetMaster;
			ret.IsetMaster = inDat.IsetMaster;

			for (int i = 0; i < 40; i++)
			{
				if (i < inDat.PsNo) ret.PsEn[i] = inDat.PsEn[i];
				else ret.PsEn[i] = 0;
				ret.Vset[i] = inDat.Vset[i];
				ret.Iset[i] = inDat.Iset[i];
				ret.StringNo[i] = inDat.StringNo[i];
				ret.PsName[i] = inDat.PsName[i];
				ret.IProtect[i] = inDat.IProtect[i];
			}

			ret.TempNo = inDat.TempNo;
			ret.TempProtectMaster = inDat.TempProtectMaster;
			for (int i = 0; i < 16; i++)
			{
				ret.TempEn[i] = inDat.TempEn[i];
				ret.TempName[i] = inDat.TempName[i];
				ret.TempProtect[i] = inDat.TempProtect[i];
			}
			ret.HW_No = inDat.HW_No;
			for (int i = 0; i < 40; i++)
			{
				ret.HW_PsEn[i] = inDat.HW_PsEn[i];
				ret.HW_PsStringIndex[i] = inDat.HW_PsStringIndex[i];
				ret.HW_PsID[i] = inDat.HW_PsID[i];
				ret.HW_PsName[i] = inDat.HW_PsName[i];
				ret.HW_IProtect[i] = inDat.HW_IProtect[i];
			}
		}


		public static bool LDS1000_Structure_Compare(PS_Conf_Struct inDatA,PS_Conf_Struct inDatB )
		{
			int err = 0;
			if(	inDatA.FileName != inDatB.FileName) err++;
			if(inDatA.FolderName != inDatB.FolderName) err++;
			if(inDatA.FullFileName != inDatB.FullFileName) err++;
			if(inDatA.LogFileName != inDatB.LogFileName) err++;


			if(inDatA.PsNo != inDatB.PsNo) err++;
			if(inDatA.IProtect_Master != inDatB.IProtect_Master) err++;
			if(inDatA.VsetMaster != inDatB.VsetMaster) err++;
			if(inDatA.IsetMaster != inDatB.IsetMaster) err++;

			for (int i = 0; i < 40; i++)
			{
				if(inDatA.PsEn[i] != inDatB.PsEn[i]) err++;
				if(inDatA.Vset[i] != inDatB.Vset[i]) err++;
				if(inDatA.Iset[i] != inDatB.Iset[i]) err++;
				if(inDatA.StringNo[i] != inDatB.StringNo[i]) err++;
				if(inDatA.PsName[i]!= inDatB.PsName[i]) err++;
				if(inDatA.IProtect[i] != inDatB.IProtect[i]) err++;
			}

			if(inDatA.TempNo != inDatB.TempNo) err++;
			if(inDatA.TempProtectMaster != inDatB.TempProtectMaster) err++;
			for (int i = 0; i < 16; i++)
			{
				if(inDatA.TempEn[i] != inDatB.TempEn[i]) err++;
				if(inDatA.TempName[i] != inDatB.TempName[i]) err++;
				if(inDatA.TempProtect[i] != inDatB.TempProtect[i]) err++;
			}
			if(inDatA.HW_No != inDatB.HW_No) err++;
			for (int i = 0; i < 40; i++)
			{
				if(inDatA.HW_PsEn[i] != inDatB.HW_PsEn[i]) err++;
				if(inDatA.HW_PsStringIndex[i] != inDatB.HW_PsStringIndex[i]) err++;
				if(inDatA.HW_PsID[i] != inDatB.HW_PsID[i]) err++;
				if(inDatA.HW_PsName[i] != inDatB.HW_PsName[i]) err++;
				if(inDatA.HW_IProtect[i] != inDatB.HW_IProtect[i]) err++;
			}
			if(err>0) return false;
			else return true;
		}


		public static string File_Seperate_From_FullName(int key, string inStr)
		{
			string folder = "", name = "";
			string[] strArray = cUtil.mSplit_Directory(inStr);
			if (strArray.Length >0)
			{
				name = strArray[strArray.Length - 1];
				for (int i = 0; i < strArray.Length - 1; i++) folder += strArray[i] + "\\";
				if (key == 0) return folder;
				else return name;
			}
			else
			{

				return "";
			}
		}

		public static string CharArray_to_String(int size, char[] inChar)
		{
			string str = "";
			for (int i = 0; i < size; i++) str += inChar[i].ToString();
			return str;
		}

		public static byte[] ROM_SAVE_Date_Convert_to_Byte_Array(string inDate)
		{
			byte[] retByte = new byte[3];
			string[] val = cUtil.char_split('.', inDate);
			string s_year = "";
			int i_year = Convert.ToInt32(val[0]);
			byte b1, b2, b3;


			s_year = Convert.ToString(i_year % 100);
			retByte[0] = Convert.ToByte(s_year);//year
			retByte[1] = Convert.ToByte(val[1]);//mon
			retByte[2] = Convert.ToByte(val[2]); //day

			b1 = Convert.ToByte((retByte[0] & 0x0F));
			b2 = Convert.ToByte(b1 << 4);
			b3 = Convert.ToByte(retByte[1] & 0x0F);

			retByte[1] = Convert.ToByte(b2 + b3);
			retByte[0] = Convert.ToByte(retByte[0] >> 4);
			return retByte;
		}

		public static string ROM_SAVE_Date_Convert_to_String(byte[] inByte)
		{
			string ret = "";
			string[] rets = new string[3];
			byte b1, b2, b3;
			b1 = Convert.ToByte(inByte[0] << 4);
			b2 = Convert.ToByte(inByte[1] & 0xF0);
			b3 = Convert.ToByte(b2 >> 4);

			rets[0] = Convert.ToString(b1 + b3);//year
			rets[1] = Convert.ToString(inByte[1] & 0x0F);//month
			rets[2] = Convert.ToString(inByte[2]);//day


			ret = rets[0] + "." + rets[1] + "." + rets[2];
			return ret;
		}

		public static float ProcessTime_String_Convert_to_Float(string inStr)
		{

			string[] vals = get_vals4(inStr);
			int hour = 0;
			int min = 0;
			int sec = 0;
			float msec = 0.0f;
			switch (vals.Length)
			{
				case 1:
					hour = Convert.ToInt32(vals[0]);
					break;
				case 2:
					hour = Convert.ToInt32(vals[0]);
					min = Convert.ToInt32(vals[1]);
					break;
				case 3:
					hour = Convert.ToInt32(vals[0]);
					min = Convert.ToInt32(vals[1]);
					sec = Convert.ToInt32(vals[2]);
					break;
				case 4:
					hour = Convert.ToInt32(vals[0]);
					min = Convert.ToInt32(vals[1]);
					sec = Convert.ToInt32(vals[2]);
					msec = Convert.ToSingle(vals[3]);
					break;
			}

			float ret_time = (float)hour * 60 * 60 + (float)min * 60 + (float)sec + msec / 100.0f;
			return ret_time;
		}

		public static string ProcessTime_Convert_to_String(float inVal)
		{
			string str = "";

			int intVal = (int)(Math.Floor(inVal));
			float reMin = (float)inVal - intVal;
			int sec = intVal % 60;
			int min = (intVal / 60) % 60;
			int hour = (intVal / 60 / 60) % 24;
			int msec = (int)(reMin * 100.0f);
			str = Number_To_Index_String(2, hour) + ":" + Number_To_Index_String(2, min) + ":" + Number_To_Index_String(2, sec) + "." + Number_To_Index_String(2, msec);
			return str;
		}

		public static string Number_To_Index_String(int inKey, int inVal)
		{
			string str = "";
			string str0 = "";
			str = inVal.ToString();
			if (str.Length < inKey)
			{
				for (int i = 0; i < inKey - str.Length; i++) str0 += "0";
			}
			return str0 + str;
		}

		public static string Find_Directory_from_FileName(string infile)
		{
			string str = "";
			string[] vals = char_split('\\', infile);
			for (int i = 0; i < vals.Length - 1; i++)
			{
				str += vals[i] + @"\";
			}
			return str;
		}

		public static string Find_Filesource_from_FileName(string infile)
		{
			string ret = "";
			string str = "";
			string[] vals = char_split('\\', infile);
			str = vals[vals.Length - 1];
			string[] vals2 = char_split('.', str);

			ret = vals2[0];
			return ret;
		}

		public static float PastTime_Convert_to_Float(DateTime STime, DateTime NTime)
		{
			float result;
			float Sf, Nf;

			Sf = STime.Day * 24 * 60 * 60 + STime.Hour * 60 * 60 + STime.Minute * 60 + STime.Second + STime.Millisecond / 1000.0f;
			Nf = NTime.Day * 24 * 60 * 60 + NTime.Hour * 60 * 60 + NTime.Minute * 60 + NTime.Second + NTime.Millisecond / 1000.0f;
			result = Nf - Sf;
			return result;
		}

		/*public static float ProcessTime_Convert_to_Float()
		{
			DateTime NTime = STime;

			float Sf, Nf;

			Sf = STime.Day * 24 * 60 * 60 + STime.Hour * 60 * 60 + STime.Minute * 60 + STime.Second;
			Nf = NTime.Day * 24 * 60 * 60 + NTime.Hour * 60 * 60 + NTime.Minute * 60 + NTime.Second;
			result = Nf - Sf;
		}
		*/

		public static float Time_Convert_String_to_Float(string inStr)
		{
			string[] vals = char_split(':', inStr);
			float result;
			float hour, min, sec;
			hour = Convert.ToSingle(vals[0]);
			min = Convert.ToSingle(vals[1]);
			sec = Convert.ToSingle(vals[2]);
			result = hour * 60.0f * 60.0f + min * 60.0f + sec;
			return result;
		}

		public static double Time_Convert_String_to_Double(string inStr)
		{
			string[] vals = char_split(':', inStr);
			float result;
			float hour, min, sec;
			hour = Convert.ToSingle(vals[0]);
			min = Convert.ToSingle(vals[1]);
			sec = Convert.ToSingle(vals[2]);
			result = hour * 60.0f * 60.0f + min * 60.0f + sec;
			return (double)result;
		}

		public static byte String_Convert_to_mHex(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToByte(vals[index]);
		}

		public static byte String_Convert_to_mByte(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToByte(vals[index]);
		}

		public static bool String_Convert_to_mBool(int index, char c, string inStr)
		{
			bool ret;
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			if (Convert.ToByte(vals[index]) == 0) ret = false;
			else ret = true;
			return ret;
		}

		public static int String_Convert_to_mIntegerSelect(int index, char c, string inStr)
		{
			int ret;
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			if (Convert.ToByte(vals[index]) == 0) ret = 0;//string
			else if (Convert.ToByte(vals[index]) == 1) ret = 1;//float
			else if (Convert.ToByte(vals[index]) == 2) ret = 2;//int
			else if (Convert.ToByte(vals[index]) == 3) ret = 3;//byte
			else ret = 0;
			return ret;
		}

		public static byte String_Hex_Convert_to_mByte(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return cUtil.MyHex2Byte(vals[index]);
		}

		public static int String_Convert_to_mInteger(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToInt32(vals[index]);
		}

		public static UInt16 String_Convert_to_mUInt16(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToUInt16(vals[index]);
		}

		public static UInt32 String_Convert_to_mUInt32(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToUInt32(vals[index]);
		}

		public static float String_Convert_to_mFloat(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return Convert.ToSingle(vals[index]);
		}

		public static string String_Convert_to_mString(int index, char c, string inStr)
		{
			string[] vals;
			vals = cUtil.char_split(c, inStr);
			return vals[index];
		}

		public static string Binary_To_String(int Dir, int Bit, byte[] inByte)
		{
			string ret = "";
			if (Dir == 0)
			{
				for (int i = 0; i < Bit; i++)
				{
					ret += inByte[Bit - 1 - i].ToString();
				}
			}
			else
			{
				for (int i = 0; i < Bit; i++)
				{
					ret += inByte[i].ToString();
				}
			}
			return ret;
		}

		public static string Binary_To_String(int Dir, int Bit, byte inByte)
		{
			string ret = "";
			int inTemp;
			if (Dir == 0)
			{
				for (int i = 0; i < Bit; i++)
				{
					inTemp = (int)(((int)inByte >> (Bit - 1 - i)) & 0x01);
					ret += inTemp.ToString();
				}
			}
			else
			{
				for (int i = 0; i < Bit; i++)
				{
					inTemp = (int)(((int)inByte >> i) & 0x01);
					ret += inTemp.ToString();
				}
			}
			return ret;
		}

		public static string Binary_To_String(int Dir, int Bit, int inkey, byte inByte)
		{
			string ret = "";
			int inTemp;
			if (Dir == 0)
			{
				for (int i = 0; i < Bit; i++)
				{
					inTemp = (int)(((int)inByte >> (Bit - 1 - i)) & 0x01);
					ret += inTemp.ToString();
				}
			}
			else
			{
				for (int i = 0; i < Bit - inkey; i++)
				{
					inTemp = (int)(((int)inByte >> i) & 0x01);
					ret += inTemp.ToString();
				}
				ret += "_";
				for (int i = inkey; i < Bit; i++)
				{
					inTemp = (int)(((int)inByte >> i) & 0x01);
					ret += inTemp.ToString();
				}
			}
			return ret;
		}

		public static byte[] Dual_Byte_To_Binary(byte inH_byte, byte inL_byte)
		{
			UInt16 inV = (UInt16)((UInt16)inH_byte << 8 + (UInt16)inL_byte);
			byte[] ret = new byte[16];

			for (int i = 0; i < 16; i++)
			{
				ret[i] = (byte)((inV & 0x01) >> i);
			}

			return ret;
		}

		public static UInt16 Dual_Byte_To_UInt16(byte inH_byte, byte inL_byte)
		{
			UInt16 ret = 0;
			ret = (UInt16)((UInt16)inH_byte * 256 + (UInt16)inL_byte);
			return ret;
		}

		public static int Dual_Byte_To_Int(byte inH_byte, byte inL_byte)
		{
			int ret = 0;
			ret = (int)inH_byte * 256 + (int)inL_byte;
			return ret;
		}

		public static float Dual_Byte_To_float(byte inH_byte, byte inL_byte)
		{
			float ret = 0.0f;
			ret = (float)((int)inH_byte * 256 + (int)inL_byte);
			return ret;
		}

		public static string Dual_Byte_To_string(byte inH_byte, byte inL_byte)
		{
			float ret = 0.0f;
			ret = (float)((int)inH_byte * 256 + (int)inL_byte);
			return Convert.ToString(ret);
		}

		public static string Float_To_String(int inKey, float inVal)
		{
			string s = "";
			switch (inKey)
			{
				case 0:
					s = String.Format("{0:0}", inVal);
					break;
				case 1:
					s = String.Format("{0:0.0}", inVal);
					break;
				case 2:
					s = String.Format("{0:0.00}", inVal);
					break;
				case 3:
					s = String.Format("{0:0.000}", inVal);
					break;
				case 4:
					s = String.Format("{0:0.0000}", inVal);
					break;
				case 5:
					s = String.Format("{0:0.00000}", inVal);
					break;
				case 6:
					s = String.Format("{0:0.000000}", inVal);
					break;
				case 7:
					s = String.Format("{0:0.0000000}", inVal);
					break;
				default:
					s = String.Format("{0:0.0000000}", inVal);
					break;
			}
			return s;
		}

		public static float Float_To_Float(int inKey, float inVal)
		{
			float OutVal;
			switch (inKey)
			{
				case 0:
					OutVal = Convert.ToSingle(String.Format("{0:0}", inVal));
					break;
				case 1:
					OutVal = Convert.ToSingle(String.Format("{0:0.0}", inVal));
					break;
				case 2:
					OutVal = Convert.ToSingle(String.Format("{0:0.00}", inVal));
					break;
				case 3:
					OutVal = Convert.ToSingle(String.Format("{0:0.000}", inVal));
					break;
				case 4:
					OutVal = Convert.ToSingle(String.Format("{0:0.0000}", inVal));
					break;
				case 5:
					OutVal = Convert.ToSingle(String.Format("{0:0.00000}", inVal));
					break;
				case 6:
					OutVal = Convert.ToSingle(String.Format("{0:0.000000}", inVal));
					break;
				case 7:
					OutVal = Convert.ToSingle(String.Format("{0:0.0000000}", inVal));
					break;
				default:
					OutVal = Convert.ToSingle(String.Format("{0:0.0000000}", inVal));
					break;
			}
			return OutVal;
		}

		public static string mSplit_FileNameOnly(string s)
		{
			string[] vals = mSplit_Directory(s);
			string ss = vals[vals.Length - 1];
			vals = get_vals_DOT(ss);
			return vals[0];
		}

		public static string[] get_vals_DOT(string line)
		{
			//char[] split = { ' ', '=', ',', '\t', '/' };
			//char[] split = { ' ', '=', ',' };
			//char[] split = { ':', ',','\t' };
			char[] split = { '.' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] mSplit_Directory(string line)
		{
			//char[] split = { ' ', '=', ',', '\t', '/' };
			//char[] split = { ' ', '=', ',' };
			//char[] split = { ':', ',','\t' };
			char[] split = { '\\' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] mSplit_Comma(string line)
		{
			char[] split = { ',' };
			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] mSplit_Semi_Colon(string line)
		{
			char[] split = { ':' };
			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] mSplit_HIPPEN(string line)
		{
			char[] split = { '-' };
			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string DateTime_To_String(DateTime inTime)
		{
			string[] s = new string[6];
			s[0] = Convert.ToString(inTime.Year) + ".";

			s[1] = Convert.ToString(inTime.Month) + ".";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(inTime.Day) + " ";
			if (s[2].Length == 2) s[2] = "0" + s[2];

			s[3] = Convert.ToString(inTime.Hour) + ":";
			if (s[3].Length == 2) s[3] = "0" + s[3];

			s[4] = Convert.ToString(inTime.Minute) + ":";
			if (s[4].Length == 2) s[4] = "0" + s[4];

			s[5] = Convert.ToString(inTime.Second);
			if (s[5].Length == 1) s[5] = "0" + s[5];

			string st = "";
			for (int i = 0; i < 6; i++) st += s[i];
			return st;
		}

		public static string Date_To_String(DateTime inTime)
		{
			string[] s = new string[3];
			s[0] = Convert.ToString(inTime.Year) + ".";

			s[1] = Convert.ToString(inTime.Month) + ".";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(inTime.Day) + " ";
			if (s[2].Length == 2) s[2] = "0" + s[2];

			string st = "";
			for (int i = 0; i < 3; i++) st += s[i];
			return st;
		}

		public static string Time_To_String(DateTime inTime)
		{
			string[] s = new string[3];

			s[0] = Convert.ToString(inTime.Hour) + ":";
			if (s[0].Length == 2) s[0] = "0" + s[0];

			s[1] = Convert.ToString(inTime.Minute) + ":";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(inTime.Second);
			if (s[2].Length == 1) s[2] = "0" + s[2];

			string st = "";
			for (int i = 0; i < 3; i++) st += s[i];
			return st;
		}

		public static string NowDateTime_To_String()
		{
			string[] s = new string[6];
			s[0] = Convert.ToString(DateTime.Now.Year) + ".";

			s[1] = Convert.ToString(DateTime.Now.Month) + ".";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(DateTime.Now.Day) + " ";
			if (s[2].Length == 2) s[2] = "0" + s[2];

			s[3] = Convert.ToString(DateTime.Now.Hour) + ":";
			if (s[3].Length == 2) s[3] = "0" + s[3];

			s[4] = Convert.ToString(DateTime.Now.Minute) + ":";
			if (s[4].Length == 2) s[4] = "0" + s[4];

			s[5] = Convert.ToString(DateTime.Now.Second);
			if (s[5].Length == 1) s[5] = "0" + s[5];

			string st = "";
			for (int i = 0; i < 6; i++) st += s[i];
			return st;
		}

		public static string NowDate_To_String()
		{
			string[] s = new string[3];
			s[0] = Convert.ToString(DateTime.Now.Year) + ".";

			s[1] = Convert.ToString(DateTime.Now.Month) + ".";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(DateTime.Now.Day) + " ";
			if (s[2].Length == 2) s[2] = "0" + s[2];

			string st = "";
			for (int i = 0; i < 3; i++) st += s[i];
			return st;
		}

		public static string NowTime_To_String()
		{
			string[] s = new string[3];
			s[0] = Convert.ToString(DateTime.Now.Hour) + ":";
			if (s[0].Length == 2) s[0] = "0" + s[0];

			s[1] = Convert.ToString(DateTime.Now.Minute) + ":";
			if (s[1].Length == 2) s[1] = "0" + s[1];

			s[2] = Convert.ToString(DateTime.Now.Second);
			if (s[2].Length == 1) s[2] = "0" + s[2];

			string st = "";
			for (int i = 0; i < 3; i++) st += s[i];
			return st;
		}

		public static string LastStr(string inString)
		{
			string s = "";
			string[] vals = cUtil.get_vals(inString);
			s = "";
			for (int i = 0; i < vals.Length; i++)
			{
				s += vals[i];
				if (i < vals.Length - 1) s += ",";
			}
			if (s == "") s = "0";
			return s;
		}

		public static string U64BitPin_To_Sring(UInt64 inVal)
		{
			string s = "";
			int[] PinArray = new int[64];

			for (int i = 0; i < 64; i++)
			{
				if (((inVal >> i) & 0x01) == 1) PinArray[i] = 1;
				else PinArray[i] = 0;
			}
			for (UInt64 i = 0; i < 64; i++)
			{
				if (PinArray[i] == 1)
				{
					s += Convert.ToString(i + 1);
					s += ",";
				}
			}
			s = LastStr(s);
			return s;
		}

		public static string UInt64toStr(int[] inVal)
		{
			string s = "";

			for (UInt64 i = 0; i < 64; i++)
			{
				if (inVal[i] == 1)
				{
					s += Convert.ToString(i + 1);
					s += ",";
				}
			}
			return s;

		}

		public static string UInt64toStr(UInt64[] inVal)
		{
			string s = "";

			for (UInt64 i = 0; i < 64; i++)
			{
				if (inVal[i] == 1)
				{
					s += Convert.ToString(i + 1);
					s += ",";
				}
			}
			return s;

		}

		public static string UInt40toStr(UInt64[] inVal)
		{
			string s = "";

			for (UInt64 i = 0; i < 40; i++)
			{
				if (inVal[i] == 1)
				{
					s += Convert.ToString(i + 1);
					s += ",";
				}
			}
			return s;

		}

		public static UInt64 UInt64toInVert(UInt64 inVal)
		{
			UInt64[] InB1 = new UInt64[64];
			UInt64[] InB2 = new UInt64[64];
			UInt64 ret = 0;


			for (int i = 0; i < 64; i++)
			{
				InB1[i] = (inVal >> (i)) & 0x01;
				InB2[i] = 0;
			}

			for (int i = 0; i < 64; i++)
			{
				InB2[i] = InB1[i] << (63 - i);
			}

			ret = 0;
			for (int i = 0; i < 64; i++)
			{
				ret += InB2[i];
			}
			return ret;
		}

		public static string N2Str(float inVal, int sufix)
		{
			string s = "";
			string[] ss = new string[2];
			ss = mSplit(Convert.ToString(inVal));
			int fKey = 0;
			if (sufix == 0)
			{
				s = ss[0];
				return s;
			}
			if (ss.Length == 1)
			{
				s = ss[0] + ".";
				for (int i = 0; i < sufix; i++) s += "0";
				return s;
			}

			if (ss[1].Length == 0) fKey = 0;
			if (ss[1].Length < sufix) fKey = 1;
			if (ss[1].Length > sufix) fKey = 2;
			switch (fKey)
			{
				case 0:
					s = ss[0] + "." + ss[1];
					//for (int i = 0; i < sufix; i++) s += "0";
					break;
				case 1:
					s = ss[0] + "." + ss[1];
					for (int i = ss[1].Length; i < sufix; i++) s += "0";
					break;
				case 2:
					s = ss[0] + "." + ss[1].Remove(sufix);
					break;
			}
			return s;
		}

		public static void mDelay_ms(long inVal)
		{
			long oTime, nTime;
			nTime = DateTime.Now.Ticks;
			oTime = nTime;
			inVal *= 18 * 100 * 5;
			for (; ; )
			{
				nTime = DateTime.Now.Ticks;
				if ((nTime - oTime) >= inVal) break;
				Application.DoEvents();
			}
		}

		public static string CanData2String(byte[] inByte)
		{
			string s = "";
			for (int i = 0; i < 8; i++)
			{
				s += cUtil.MyDec2Hex(inByte[i]);
				if (i < 7) s += ",";
			}
			return s;
		}

		public static string CanData2String(int inKey, byte[,] inByte)
		{
			string s = "";
			for (int i = 0; i < 8; i++)
			{
				s += cUtil.MyDec2Hex(inByte[inKey, i]);
				if (i < 7) s += ",";
			}
			return s;
		}

		public static void String2CanData(ref byte[] cDat, string s)
		{
			string[] vals;
			vals = get_vals(s);
			for (int i = 0; i < vals.Length; i++) cDat[i] = Convert.ToByte(cUtil.MyHex2Dec(vals[i]));
			if (vals.Length < 8) for (int i = vals.Length; i < 8; i++) cDat[i] = 0;
		}

		public static void String2CanData(int inKey, ref byte[,] cDat, string s)
		{
			string[] vals;
			vals = get_vals(s);
			for (int i = 0; i < vals.Length; i++) cDat[inKey, i] = Convert.ToByte(cUtil.MyHex2Dec(vals[i]));
			if (vals.Length < 8) for (int i = vals.Length; i < 8; i++) cDat[inKey, i] = 0;
		}

		public static string MyDec2Hex(byte inVal)
		{
			string s = BitConverter.ToString(BitConverter.GetBytes(inVal)).Remove(2);
			return s;
		}

		public static string MyDec2Hex(UInt16 inVal)
		{
			byte inVal1;
			byte inVal2;
			inVal1 = (byte)((inVal & 0xFF00) >> 8);
			inVal2 = (byte)(inVal & 0x00FF);
			string s = BitConverter.ToString(BitConverter.GetBytes(inVal1)).Remove(2) + BitConverter.ToString(BitConverter.GetBytes(inVal2)).Remove(2);

			return s;
		}

		public static string MyDec2Hex(UInt32 inVal)
		{
			byte inVal1;
			byte inVal2;
			byte inVal3;
			byte inVal4;
			inVal1 = (byte)((inVal & 0xFF000000) >> 24);
			inVal2 = (byte)((inVal & 0x00FF0000) >> 16);
			inVal3 = (byte)((inVal & 0x0000FF00) >> 8);
			inVal4 = (byte)(inVal & 0x000000FF);
			string s = BitConverter.ToString(BitConverter.GetBytes(inVal1)).Remove(2) + BitConverter.ToString(BitConverter.GetBytes(inVal2)).Remove(2)
				+ BitConverter.ToString(BitConverter.GetBytes(inVal3)).Remove(2) + BitConverter.ToString(BitConverter.GetBytes(inVal4)).Remove(2);

			return s;
		}

		public static string Dec2Hex(UInt32 inVal)
		{
			string s = BitConverter.ToString(BitConverter.GetBytes((byte)(inVal >> 28))).Remove(2);
			s += BitConverter.ToString(BitConverter.GetBytes((byte)(inVal >> 16))).Remove(2);
			s += BitConverter.ToString(BitConverter.GetBytes((byte)(inVal >> 8))).Remove(2);
			s += BitConverter.ToString(BitConverter.GetBytes((byte)(inVal))).Remove(2);
			return s;
		}

		public static string Dec2Hex(UInt16 inVal)
		{
			string s = BitConverter.ToString(BitConverter.GetBytes((byte)(inVal >> 8))).Remove(2);
			s += BitConverter.ToString(BitConverter.GetBytes((byte)(inVal))).Remove(2);
			return s;
		}

		public static string Dec2Hex(Byte inVal)
		{
			string s = BitConverter.ToString(BitConverter.GetBytes(inVal)).Remove(2);
			return s;
		}

		public static UInt32 MyHex2Dec(byte inSize, string inStr)
		{
			char[] c = inStr.ToCharArray();
			int b1 = 0, b2 = 0;
			UInt32 ret = 0, retT;
			if (inStr.Length == 8)
			{
				for (int i = 0; i < 4; i++)
				{
					if (inStr.Length == 1)
					{
						b1 = Char2Byte(c[i * 2]);
						b2 = 0;
					}
					else
					{
						b1 = Char2Byte(c[i * 2]);
						b2 = Char2Byte(c[i * 2 + 1]);
					}
					retT = (UInt32)((b1 << 4) + b2);
					ret += retT << (3 - i) * 8;
				}
			}
			return ret;
		}

		public static int MyHex2Dec(string inStr)
		{
			char[] c = inStr.ToCharArray();
			int b1 = 0, b2 = 0;

			if (inStr.Length == 1)
			{
				b1 = Char2Byte(c[0]);
				b2 = 0;
			}
			else
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
			}
			return b1 * 16 + b2;
		}

		public static UInt16 MyHex2UInt16(string inStr)
		{
			char[] c = inStr.ToCharArray();
			int[] b = new int[4];
			b[0] = 0;
			b[1] = 0;
			b[2] = 0;
			b[3] = 0;
			for (int i = 0; i < inStr.Length; i++)
			{
				b[i] = Convert.ToInt16(Char2Byte(c[i]));
			}
			return Convert.ToUInt16(b[3] * 256 * 16 + b[2] * 256 + 16 + b[1] * 16 + b[0]);
		}

		public static byte MyHex2DecByte(string inStr)
		{
			char[] c = inStr.ToCharArray();
			int b1 = 0, b2 = 0;

			if (inStr.Length == 1)
			{
				b1 = Char2Byte(c[0]);
				b2 = 0;
			}
			else
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
			}
			return Convert.ToByte(b1 * 16 + b2);
		}

		public static byte MyHex2Byte(string inStr)
		{
			char[] c = inStr.ToCharArray();
			int b1 = 0, b2 = 0, b3 = 0, b4 = 0;

			if (inStr.Length == 1)
			{
				b1 = Char2Byte(c[0]);
				b2 = 0;
			}
			else if (inStr.Length == 2)
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
			}
			else
			{
				b3 = Char2Byte(c[0]);
				b4 = Char2Byte(c[1]);
				b1 = Char2Byte(c[2]);
				b2 = Char2Byte(c[3]);
			}
			return (byte)(b1 * 16 + b2 + b3 + b4);
		}

		public static UInt16 My2Hex_ToByte(string inStr)
		{
			char[] c = inStr.ToCharArray();
			int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0;

			if (inStr.Length == 1)
			{
				b1 = Char2Byte(c[0]);
				b2 = 0;
				b3 = 0;
				b4 = 0;
			}
			else if (inStr.Length == 2)
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
				b3 = 0;
				b4 = 0;
			}
			else if (inStr.Length == 3)
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
				b3 = Char2Byte(c[2]);
				b4 = 0;
			}
			else if (inStr.Length == 4)
			{
				b1 = Char2Byte(c[0]);
				b2 = Char2Byte(c[1]);
				b3 = Char2Byte(c[2]);
				b4 = Char2Byte(c[3]);
			}
			else
			{
				b5 = Char2Byte(c[0]);
				b6 = Char2Byte(c[1]);
				b1 = Char2Byte(c[2]);
				b2 = Char2Byte(c[3]);
				b3 = Char2Byte(c[4]);
				b4 = Char2Byte(c[5]);
			}
			return (UInt16)(b5 + b6 + b1 * 4096 + b2 * 256 + b3 * 16 + b4);
		}

		public static int Char2Byte(char c)
		{
			int rret = 0;

			switch (c)
			{
				case '0':
					rret = 0; break;
				case '1':
					rret = 1; break;
				case '2':
					rret = 2; break;
				case '3':
					rret = 3; break;
				case '4':
					rret = 4; break;
				case '5':
					rret = 5; break;
				case '6':
					rret = 6; break;
				case '7':
					rret = 7; break;
				case '8':
					rret = 8; break;
				case '9':
					rret = 9; break;
				case 'A':
				case 'a':
					rret = 10; break;
				case 'B':
				case 'b':
					rret = 11; break;
				case 'C':
				case 'c':
					rret = 12; break;
				case 'D':
				case 'd':
					rret = 13; break;
				case 'E':
				case 'e':
					rret = 14; break;
				case 'F':
				case 'f':
					rret = 15; break;
			}

			return rret;
		}

		public static string[] mSplit(string line)
		{
			char[] split = { '.', '\t' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] get_vals(string line)
		{
			char[] split = { ':', ',', '\t' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] get_vals2(string line)
		{
			//char[] split = { ' ', '=', ',', '\t', '/' };
			char[] split = { ' ', '=', ',', '\t' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] get_vals3(string line)
		{
			char[] split = { ' ', '=', ',', '\t' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string[] char_split(char inchar, string line)
		{
			char[] split = { ' ' };
			split[0] = inchar;

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		

		public static string[] get_vals4(string line)
		{
			char[] split = { ':', '.' };

			return line.Split(split, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string Result_To_String(int Val)
		{
			string ret = "";
			switch (Val)
			{
				case 0:
					ret += "Not Test";
					break;
				case 1:
					ret += "OK";
					break;
				case 2:
					ret += "NG";
					break;
			}
			return ret;
		}

		public static int Result_To_Integer(string inStr)
		{
			int ret = 0;
			switch (inStr)
			{
				case "Not Test":
					ret = 0;
					break;
				case "OK":
					ret = 1;
					break;
				case "NG":
					ret = 2;
					break;
			}
			return ret;
		}

		public static float Convert_16Bit_To_Float(float inMax, UInt16 inVal)
		{
			float ret;
			float inTemp;
			inTemp = (float)inVal;
			ret = inTemp / 65535.0f * inMax;
			return ret;
		}

		

		public static float Time_Convert_to_Float(DateTime STime, DateTime NTime) //함수3. Util에 있음...
		{
			float result;
			float Sf, Nf;

			Sf = STime.Day * 24 * 60 * 60 + STime.Hour * 60 * 60 + STime.Minute * 60 + STime.Second + STime.Millisecond / 1000.0f;
			Nf = NTime.Day * 24 * 60 * 60 + NTime.Hour * 60 * 60 + NTime.Minute * 60 + NTime.Second + NTime.Millisecond / 1000.0f;
			result = Nf - Sf;
			return result;
		}


		public static byte[] Data_Convert_from_Float_to_Array(int key, float inVale)
		{
			byte[] ret = new byte[2];
			UInt16 temp;
			switch (key)
			{
				case 0://VS
					temp = (UInt16)(inVale * 10.0f);
					ret = BitConverter.GetBytes(temp);
					break;					
				case 1://IS
					temp = (UInt16)(inVale * 10.0f);
					ret = BitConverter.GetBytes(temp);
					break;
				case 2://sd
					temp = (UInt16)(inVale * 1000.0f);
					ret = BitConverter.GetBytes(temp);
					break;
			}
			return ret;
		}

		public static float Data_Convert_from_BYTE_Array_to_Float(int key, byte[] inD)
		{
			float ret=0.0f;
			float f1,f2;
			UInt16 temp;
			switch (key)
			{
				case 0://VS
					f1 = (float)inD[0]*256.0f; 
					f2 = (float)inD[1];
					ret = (f1 + f2)*0.1f;
					break;
				case 1://IS
					temp = BitConverter.ToUInt16(inD, 0);
					f1 = (float)inD[0]*256.0f;
					f2 = (float)inD[1];
					ret = (f1 + f2) * 0.1f;
					break;
				case 2://SD
					f1 = (float)inD[0] * 256.0f;
					f2 = (float)inD[1];
					ret = (f1 + f2)*0.1f;
					break;
				case 3://etc
					f1 = (float)inD[0] * 256.0f;
					f2 = (float)inD[1];
					ret = f1 + f2;
					break;
			}
			return ret;
		}
	}

}
